# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: tu@tulrich.com (Thatcher Ulrich)

"""Dump ~all logged data.
"""


import datetime
import os
from django.utils import simplejson
from google.appengine.api import users
from google.appengine.ext import db
import distro
import analysis


def handler(app):
  """
  """
  if not users.is_current_user_admin():
    app.response.out.write('admin only | <a href="%s">login</a>' %
                           users.create_login_url(app.request.uri))
    return

  q = db.GqlQuery("SELECT * FROM Report ORDER BY timestamp DESC LIMIT 10000")
  reports = q.fetch(10000)
  records = []
  for r in reports:
    record = { 'timestamp': r.timestamp.strftime("%Y-%m-%d %H:%M:%S") }
    for k in r.dynamic_properties():
      v = getattr(r, k)
      record[k] = v
    records.append(record)

  app.response.headers['Content-Type'] = "text/plain; charset=utf-8"
  app.response.out.write(simplejson.dumps(records))
